<?php
if(!defined('IN_MANU')){ 
	die('[ERROR] You cannot load this page directly !!!');
}

class ProfileManager 
{
	private $db = null;
	
	function __construct($db)
	{
		$this->db = $db;
	}
	
	/*
	 * @description:	Just get the name of profiles
	 */
	public function getBasicInfo()
	{
		//this query is for admin user
		//TODO:	need to modify
		$query = 'SELECT DISTINCT profile 
				  FROM dba_profiles';
		
		$res = $this->db->getData($query, 'profile');
		
		return $res;
	}
	public function checkProfile($priv){
		$userName  = $this->db->get('userManager')->getCurrentUser();
		$query1 = 'select * 
		           from dba_sys_privs 
		           where grantee = '. var_export(strtoupper($userName), true) .  
		           " and privilege = " . var_export(strtoupper($priv), true);
		$res1 = $this->db->getData($query1, 'user');
		$query2 = 'select * 
		           from dba_role_privs 
		           where grantee = '.var_export(strtoupper($userName), true). " and granted_role = 'DBA'";
		$res2 = $this->db->getData($query2, 'user');
		if(count($res1) == 0 && count($res2) == 0)
			return false;
		else 
			return true;
		
	}
	public function checkCreateProfile(){
		$userName  = $this->db->get('userManager')->getCurrentUser();
		$query1 = 'select * from dba_sys_privs where grantee = '. var_export(strtoupper($userName), true).  " and privilege = 'CREATE PROFILE'";
		$res1 = $this->db->getData($query1, 'user');
		$query2 = 'select * from dba_role_privs where grantee = '.var_export(strtoupper($userName), true). " and granted_role = 'DBA'";
		$res2 = $this->db->getData($query2, 'user');
		if(count($res1) == 0 && count($res2) == 0)
			return false;
		else 
			return true;
	}
	/*
	 * @description:	Get general info(session_per_user, connect_time, idle_time) of profiles
	 */
	public function getGenInfo($name)
	{
		$query = 'SELECT DISTINCT RESOURCE_NAME, LIMIT
				  FROM dba_profiles
                  WHERE profile = ' . var_export($name, true);
		
		$res = $this->db->getData($query, 'profile');
		
		return $res;
	}
	
	/*
	 * @description:	Get users of one profile
	 */
	public function getUserOf($profile)
	{
		$query = 'SELECT DISTINCT USERNAME
				  FROM dba_users
				  WHERE profile = ' . var_export($profile, true);
		
		$res = $this->db->getData($query, 'profile');
		 
		return $res;
	}
	public function addProfile($name, $session, $connect, $idle){
		$query = "CREATE PROFILE $name LIMIT 
				SESSIONS_PER_USER     $session 
				CONNECT_TIME          $connect
				IDLE_TIME  			  $idle	";
		
		$res = $this->db->dbQuery($query);
		
		if ($res == false) {
			$_SESSION['message'] = 'error';
		}
		else {
			$_SESSION['message'] = 'add';
		}		
	}
	public function editProfile($name, $session, $connect, $idle){
		$query = "ALTER PROFILE $name LIMIT 
				SESSIONS_PER_USER     $session 
				CONNECT_TIME          $connect
				IDLE_TIME  			  $idle	";
		
		$res = $this->db->dbQuery($query);
		
		if ($res == false) {
			$_SESSION['message'] = 'error';
		}
		else {
			$_SESSION['message'] = 'edit';
		}		
	}
	
	/*
	 * @description:	Delete profile
	 */
	public function deleteProfile($profile)
	{
		$query = "DROP PROFILE $profile CASCADE";
		
		$res = $this->db->dbQuery($query);
		
		if ($res == false) {
			$_SESSION['message'] = 'error';
		}
		else {
			$_SESSION['message'] = 'delete';
		}
		
		return $res;
	}
}